package com.TPNexus.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;

import org.hibernate.Query;
import org.hibernate.Session;

import com.TPNexus.model.CatalogItem;
import com.TPNexus.model.PurchaseOrder;
import com.TPNexus.model.PurchaseOrderLineItem;
import com.TPNexus.model.ShoppingCart;
import com.TPNexus.model.ShoppingCartItem;
import com.TPNexus.model.User;

@Named
@ApplicationScoped
public class PurchaseOrderDAO {
	private Session session=HibernateInitial.getSession();
	public int createPurchaseOrder(ShoppingCart shoppingCart, Date deliverydate) {
		int result;
		try {
			session.beginTransaction();
			Set<Integer> supplier_num = new HashSet<Integer>();
			for (ShoppingCartItem shoppingCartItem : shoppingCart
					.getShoppingCartItems()) {
				supplier_num.add(shoppingCartItem.getCatalogItem()
						.getSupplierId());
			}// how many PO should be generated

			for (Integer integer : supplier_num) {
				PurchaseOrder PO = new PurchaseOrder();
				User seller = (User) session.get(User.class, (long) integer);
				PO.setSeller(seller);
				User buyer = (User) session.get(User.class,
						shoppingCart.getUserId());
				PO.setAuthor(buyer);
				PO.setStatus("NEW");
				PO.setPurchaseorderdocno("PO-" + System.currentTimeMillis());
				for (ShoppingCartItem shoppingCartItem : shoppingCart
						.getShoppingCartItems()) {
					if (shoppingCartItem.getCatalogItem().getSupplierId() == integer) {
						PurchaseOrderLineItem POL = new PurchaseOrderLineItem();
						POL.setCatalogitem(shoppingCartItem.getCatalogItem());
						POL.setDeliverydate(deliverydate);
						POL.setQty(shoppingCartItem.getQty());
						
						PO.getPolineitems().add(POL);
					}
				}
				session.merge(PO);
				
			}
			session.getTransaction().commit();
			result = 1;
		} catch (RuntimeException e) {
			result = 0;
			session.getTransaction().rollback();
			System.out.println(e.toString());
			throw e;
		}
//		session.close();
		return result;
		
	}

	public ArrayList<PurchaseOrder> getPOListBuyerView(long userid,
			String status) {
		session.beginTransaction();
		ArrayList<PurchaseOrder> list = new ArrayList<PurchaseOrder>();
		Query query = session
				.createQuery("from PurchaseOrder po where po.author.userid="
						+ userid + " and po.status='" + status + "'");
		list = (ArrayList<PurchaseOrder>) query.list();

		session.getTransaction().commit();
		return list;
	}

	public ArrayList<PurchaseOrder> getPOListSellerView(long userid,
			String status) {
		session.beginTransaction();
		ArrayList<PurchaseOrder> list = new ArrayList<PurchaseOrder>();
		Query query = session
				.createQuery("from PurchaseOrder po where po.seller.userid="
						+ userid + " and po.status='" + status + "'");
		list = (ArrayList<PurchaseOrder>) query.list();

		session.getTransaction().commit();
		return list;
	}

	public PurchaseOrder getPODetailById(long id) {
		session.beginTransaction();
		PurchaseOrder purchaseOrder = (PurchaseOrder) session.get(
				PurchaseOrder.class, id);

		session.getTransaction().commit();
		return purchaseOrder;
	}
}
